<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Request;
use app\index\model\Nav;
use app\index\model\Type;
use app\index\model\Specific;
use app\index\model\KnowUserLog;
use app\index\service\SubService;
use app\index\service\UserService;
use app\index\model\KnowUserRecord;
use app\index\service\CourseService;
use app\index\model\KnowUserTraining;
use app\index\model\KnowUserQuestion;
use app\index\model\Course as CourseModel;



class Course  extends Common
{
	public function index(Request $request)
	{
		$param = $request->param();
		$data = CourseService::getCourseDetail($param['id']);
		if(!$param['id'] || !$data) die($this->checkParamId());
		$navData = Nav::getOneData($data['nav_id']);		
		$typeData = Type::getOneData($data['type_id']);
		$sData = Specific::getOneData($data['s_id']);
		$courseData = CourseModel::getWhereData(['s_id' => $data['s_id']]);
		$courseData = json_decode( json_encode($courseData), true);
		list($courseData, $arr) = CourseService::disposeCourseLog(['s_id' => $data['s_id'],'type_id'=>$data['type_id']], $courseData);
		if ( user() ){
			if ($data['status'] != 0){
				$fetch = returnCourseFetch($data['type']);
				if ($fetch == 'video')
				{
					if ($param['seek']){
						$data['record'] = $param['seek'];
					}else{
						$record = KnowUserRecord::getDetail(['id'=>$param['id']]);
						$data['record'] = $record ? $record['record']-10 : 0;
					}
					if (!$data['vid']) $fetch = 'novid';
				}
				else if ($fetch == 'practice')
				{
					if ($param['sort'] > $data['points']) $param['sort'] = $data['points'];
					$data['ques'] = SubService::getQuestions($data, ['s_id'=>$data['s_id'], 'sort'=> $param['sort']]);
					if(!$data['ques'][0]) die($this->checkParamId());
					$param['sort'] = $data['ques'][4] ?: 1;
				}
				else if ($fetch == 'challenge')
				{
					$data['training'] = KnowUserTraining::getCourseUserTraing($param);
				}
				else if ($fetch == 'document')
				{
					UserService::changeUserLog($data);
				}
				$nextData = CourseService::getPrevNextData($data);
			} else {
				$navData = CourseService::getNavPrice($navData);
				$fetch = 'permission';
			}
		} else {
			$fetch = 'nouser';
		}
		return $this->fetch('',[
			'fetch' => $fetch,
			'param' => $param,
			'data' => $data,
			'sData' => $sData,
			'navData' => $navData,
			'nextData' => $nextData,
			'typeData' => $typeData,
			'courseData' => $courseData,
			'title' => $data['course_name'],
		]);
	}

	public function pages(Request $request)
	{
		$param = $request->param();
		$data = CourseService::getCourseDetail($param['id']);
		if(!$param['id'] || !$data) die($this->checkParamId());
		if ( user() ){
			if ($data['status'] != 0){
				$fetch = $param['fetch'];
				if ($param['sort'] > $data['points']) $param['sort'] = $data['points'];
				$data['ques'] = SubService::getQuestions($data, ['s_id'=>$data['s_id'], 'sort'=> $param['sort']]);
				$param['sort'] = $data['ques'][4] ?: 1;
				$nextData = CourseService::getPrevNextData($data);
			} else {
				$fetch = 'permission';
			}
		} else {
			$fetch = 'nouser';
		}
		return $this->fetch($fetch,[
			'data' => $data,
			'param' => $param,
			'nextData' => $nextData,
		]);
	}

	public function search(Request $request)
	{
		$param = $request->param();
		$data = CourseModel::getAllData($param);
		return $this->fetch('',[
			'data' => $data,
			'param' => $param,
		]);
	}

	public function training(Request $request)
	{
		$param = $request->param();
		$data = CourseService::getCourseDetail($param['id']);
		$user = user();
		if (!$user) return json(['code' => 400, 'msg' => '请先登录']);
		if (!$data) return json(['code' => 400, 'msg' => '参数错误']);
		if ($data['status'] == 1)
		{
			KnowUserTraining::insert([
				'user_id' => $user['id'],
				'course_id' => $data['course_id'],
				'nav_id' => $data['nav_id'],
				'type_id' => $data['type_id'],
				's_id' => $data['s_id'],
				'content' => $param['content'],
				'time' => date('Y-m-d H:i:s'),
			]);
			KnowUserLog::addData([
				'id' => $data['course_id'],
				'status' => 1,
				'points' => $data['points'],
				'origin_points' => $data['points'],
			]);
			return json(['code' => 200]);
		}else{
			return json(['code' => 400, 'msg' => '没有权限，请先购买']);
		}
	}


	public function question(Request $request)
	{
		$param = $request->param();
		$data = CourseService::getCourseDetail($param['id']);
		$user = user();
		if (!$user) return json(['code' => 400, 'msg' => '请先登录']);
		if (!$data) return json(['code' => 400, 'msg' => '参数错误']);
		if ($data['status'] == 1)
		{
			KnowUserQuestion::insert([
				'user_id' => $user['id'],
				'course_id' => $data['course_id'],
				'nav_id' => $data['nav_id'],
				'type_id' => $data['type_id'],
				's_id' => $data['s_id'],
				'content' => $param['content'],
				'time' => date('Y-m-d H:i:s'),
			]);
			return json(['code' => 200]);
		}else{
			return json(['code' => 400, 'msg' => '没有权限，请先购买']);
		}
	}


	public function questionlists(Request $request)
	{
		$param = $request->param();
		$data = CourseService::getCourseDetail($param['id']);
		if (!$data) return json(['code' => 400, 'msg' => '参数错误']);
		list($lists, $render) = CourseService::getQuesLists($param);
		return $this->fetch('', [
			'lists' => $lists,
			'render' => $render,
		]);
	}

	public function lists(Request $request)
	{
		$param = $request->param();
		if (!$param['id'] || !$param['sid']) return json(['code'=>400,'msg'=>'请求无效']);
		list($courseData, $sData) = CourseService::getSPrevNextData($param);
		return json([
			'code' => 200,
			's' => $sData['s_id'],
			'name' => $sData['s_name'],
			'html' => $this->fetch('lists',[
				'param' => $param,
				'sData' => $sData,
				'courseData' => $courseData,
			]),
		]);
	}
}